package com.ruoyi.project.ins.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.ins.domain.InsPromotion;
import com.ruoyi.project.ins.service.IInsPromotionService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/ins/promotion")
public class InsPromotionController extends BaseController {
    @Autowired
    private IInsPromotionService insPromotionService;

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<InsPromotion> util = new ExcelUtil<>(InsPromotion.class);
        util.importTemplateExcel(response, "推广用户数据");
    }

    @PreAuthorize("@ss.hasPermi('ins:promotion:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsPromotion insPromotion)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            insPromotion.setCreateBy(username);
        }
        startPage();
        List<InsPromotion> list = insPromotionService.selectInsPromotionList(insPromotion);
        TableDataInfo rsp = getDataTable(list);
        Map<String, Object> totalMap = insPromotionService.selectSumPromotion(insPromotion);
        rsp.setSumInfo(totalMap);
        return rsp;
    }

    @PreAuthorize("@ss.hasPermi('ins:promotion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(insPromotionService.selectAccountById(id));
    }

    @Log(title = "ins推广用户", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('ins:promotion:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, InsPromotion insPromotion)
    {
        String username = getUsername();
        if (!username.equalsIgnoreCase("admin")) {
            insPromotion.setCreateBy(username);
        }
        List<InsPromotion> list = insPromotionService.selectInsPromotionList(insPromotion);
        ExcelUtil<InsPromotion> util = new ExcelUtil<>(InsPromotion.class);
        util.exportExcel(response, list, "推广用户数据");
    }

    @Log(title = "ins推广用户", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ins:promotion:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<InsPromotion> util = new ExcelUtil<>(InsPromotion.class);
        List<InsPromotion> registerList = util.importExcel(file.getInputStream());
        String operateName = getUsername();
        String message = insPromotionService.importAccount(registerList, operateName);
        return success(message);
    }

    @PreAuthorize("@ss.hasPermi('ins:promotion:add')")
    @Log(title = "ins推广用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody InsPromotion insPromotion)
    {
        InsPromotion ins = insPromotionService.selectByUserId(insPromotion.getUserId());
        if (ObjectUtils.isNotEmpty(ins)) {
            return error("该用户id已存在：" + insPromotion.getUserId());
        }
        insPromotion.setCreateBy(getUsername());
        insPromotion.setStatus("0");
        return toAjax(insPromotionService.insertInsPromotion(insPromotion));
    }

    @PreAuthorize("@ss.hasPermi('ins:promotion:edit')")
    @Log(title = "ins推广用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody InsPromotion insPromotion)
    {
        InsPromotion ins = insPromotionService.selectByUserId(insPromotion.getUserId());
        // 当库里存在新userId，且id记录与修改目标的id不一样，说明修改的新userId数据库已存在
        if (ObjectUtils.isNotEmpty(ins) && !Objects.equals(ins.getId(), insPromotion.getId())) {
            return error("该用户id已存在：" + insPromotion.getUserId());
        }
        insPromotion.setUpdateBy(getUsername());
        return toAjax(insPromotionService.updateInsPromotion(insPromotion));
    }

    @PreAuthorize("@ss.hasPermi('ins:promotion:remove')")
    @Log(title = "ins推广用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deleteIds}")
    public AjaxResult remove(@PathVariable Long[] deleteIds)
    {
        return toAjax(insPromotionService.deleteByIds(deleteIds));
    }
}
